﻿package net.wangit.jdbc.sqltype;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class BlobSqlType extends NullableSqlType {

	private static final Log log = LogFactory.getLog(BlobSqlType.class);
	private static final long serialVersionUID = 1L;

	public BlobSqlType() {
	}

	public String get(ResultSet rs, String name) throws SQLException {
		if (rs.wasNull())
			return null;
		else
			return "";
	}

	protected void setNotNull(PreparedStatement st, String value, int index)
			throws SQLException {
		try {
			File file = new File(value);
			BufferedInputStream in = new BufferedInputStream(
					new FileInputStream(file));
			st.setBinaryStream(index, in, (int) file.length());
		} catch (Exception e) {
			log.error(e);
			throw new SQLException(e.getMessage());
		}
	}

	public int sqlType() {
		return Types.BLOB;
	}

}
